﻿using System;
using System.Collections.Generic;
using System.Data.OleDb;
using System.Linq;
using System.Text;

namespace VIFON.DAL
{
    public class DABill
    {
        /// <summary>
        /// get Bills  by Customer
        /// </summary>
        /// <param name="customerCode"></param>
        /// <returns></returns>
        internal List<Bill> GetBills(int customerId)
        {
            try
            {
                using (var db = new DBVIFONDataContext())
                {
                    var temp = db.Bills.Where(o => o.CustomerId == customerId);
                    return temp.Count() > 0 ? temp.ToList() : null;
                }
            }
            catch
            {
                return null;
            }
        }
        internal List<Bill> GetBillsList()
        {
            try
            {
                using (var db = new DBVIFONDataContext())
                {
                    var temp = db.Bills;
                    return temp.Count() > 0 ? temp.ToList() : null;
                }
            }
            catch
            {
                return null;
            }
        }

        internal bool Delete(int id)
        {
            try
            {
                using (var db = new DBVIFONDataContext())
                {
                    var temp = db.Bills.FirstOrDefault(o => o.Id == id);
                    if (temp == null)
                        return false;
                    db.Bills.DeleteOnSubmit(temp);
                    db.SubmitChanges();
                    return true;
                }
            }
            catch
            {
                return false;
            }
        }
        internal bool Insert(Bill boBill)
        {
            try
            {
                using (var db = new DBVIFONDataContext())
                {
                    db.Bills.InsertOnSubmit(boBill);
                    db.SubmitChanges();
                    return true;
                }
            }
            catch
            {
                return false;
            }
        }

        internal bool Update(Bill boBill)
        {
            try
            {
                using (var db = new DBVIFONDataContext())
                {
                    var temp = db.Bills.FirstOrDefault(o => o.Id == boBill.Id);
                    if (temp == null)
                        return false;
                    temp.CarId = boBill.CarId;
                    temp.CreatedDate = boBill.CreatedDate;
                    temp.CustomerId = boBill.CustomerId;
                    temp.Notation = boBill.Notation;
                    temp.Stan = boBill.Stan;
                    db.SubmitChanges();
                    return true;
                }
            }
            catch
            {
                return false;
            }
        }
    }
}
